package cn.edu.gcu.giftems.service;

import cn.edu.gcu.giftems.daos.GiftDao;
import cn.edu.gcu.giftems.pojos.Gift;
import cn.edu.gcu.giftems.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
@Transactional
public class GiftService {

    private GiftDao giftDao = new GiftDao();

    public GiftDao getGiftDao() {
        return giftDao;
    }

    public void setGiftDao(GiftDao giftDao) {
        this.giftDao = giftDao;
    }

    public void save(Gift gift) {
        giftDao.save(gift);
    }

    //根据礼品编号查询礼品信息
    public Gift findById(int id) {
        return giftDao.finById(id);
    }

    //更新礼品信息
    public void update(Gift gift) {
        giftDao.update(gift);
    }

    //根据礼品id删除礼品
    public void delete(Gift gift) {
        giftDao.delete(gift);
    }

    public int findCount() {
        return giftDao.findCount();
    }


    //分页查询
    public PageUtils<Gift> findByPage(int currPage) {
        PageUtils<Gift> PageUtils = new PageUtils<Gift>();
        //封装当前的页数
        PageUtils.setCurrPage(currPage);
        //封装每页记录数
        int pageSize = 10;
        PageUtils.setPageSize(pageSize);
        //封装总记录数
        int totalCount = giftDao.findCount();
        PageUtils.setTotalCount(totalCount);
        //封装总页数
        int totalPage = totalCount%pageSize == 0?totalCount/pageSize:(totalCount/pageSize+1);
        PageUtils.setTotalPage(totalPage);
        //封装每页显示的数据
        int begin = (currPage - 1)*pageSize;
        List<Gift> list = giftDao.findByPage(begin,pageSize);
        PageUtils.setList(list);
        return PageUtils;
    }

    public List<Gift> findAll() {
        return giftDao.findAll();
    }

    public PageUtils<Gift> findByParameter(Integer currPage, String filter_name, String simple_filter_value) {
        PageUtils<Gift> PageUtils = new PageUtils<Gift>();
        PageUtils.setCurrPage(currPage);
        PageUtils.setTotalPage(1);
        List<Gift> list = giftDao.findByParameter(filter_name,simple_filter_value);
        PageUtils.setList(list);
        int totalCount = list.size();
        PageUtils.setTotalCount(totalCount);
        return PageUtils;
    }
}
